
local mt = class.panel.create('',0,100,500,500)
mt:set_view_port(true)
mt.init_w = 350
mt.init_h = 30
mt.count = 20
mt.gap = 42
mt.gap_w = 200
mt.texts = {}
mt.icon_w = 42
mt.icon_h = 42
mt.speed = 400
mt.remove_time = 10
mt.hide_time = 0.2
mt.init_icon = [[ui\遮罩.tga]]
mt.icon = [[ui\喇叭.tga]]
mt.info = {}

--发送
function mt:send_msg(msg,art)
    local art = art or mt.icon
    self.info[#self.info+1] = {msg,art}
end

function mt:init_panel()
    for a,panel in ipairs(self.texts) do
        panel:remove()
    end
    
    for a=1,self.count do
        local ui = self:add_panel('',self.icon_w/2,0,self.w,self.init_h)
        ui:add_panel(self.init_icon,self.icon_w/2,0,self.init_w,self.init_h)
        ui.icon = ui:add_panel(self.icon,0,(self.init_h - self.icon_h)/2,self.icon_w,self.icon_h)
        ui.text = ui:add_text('',self.icon_w+2,0,nil,nil,12,4)
        ui:hide()
        self.texts[self.count - a + 1] = ui
    end
end
mt:init_panel()

mt.clock = 0
function mt:update(frame)
    for i,ui in ipairs(self.texts) do
        if ui:is_show() and ui.alpha_time then
            ui.alpha_time = ui.alpha_time - frame
            local alpha = ui.alpha_time*255/self.hide_time
            if alpha<255 and alpha>0 then
                ui:set_alpha(alpha)
            end
            if ui.alpha_time<=0 then
                ui:hide()
            end
            local point = ui.target_point
            if point then
                local x1,y1 = ui.x,ui.y
                local x2,y2 = point[1],point[2]
                if x1~=x2 then
                    x1 = math.max(x1 - self.speed*frame,x2)
                end
                if y1~=y2 then
                    y1 = math.max(y1 - self.speed*frame,y2)
                end
                ui:set_position(x1,y1)
            end
        end
    end
    self.clock = self.clock + frame
    if #self.info>0 then
        if self.clock>self.gap/self.speed then
            self.clock = 0
            local data = table.remove(self.info,1)
            local msg,art = data[1],data[2]
            local ui = table.remove(self.texts,#self.texts)
            ui.icon:set_normal_image(art)
            ui.text:set_text(msg)
            ui.alpha_time = self.remove_time
            ui:set_alpha(255)
            ui:hide()
            ui:set_position(self.gap_w,self.h)
            ui:show()
            table.insert(self.texts,1,ui)
            for i,ui in ipairs(self.texts) do
                local y = -math.min(i,10) * self.gap
                ui.target_point = {0,self.h + y}
                ui:set_level(#self.texts - i + 1)
            end

        end
    end
end

ac.hardware:event '界面绘制'(function(_,frame)
    mt:update(frame)
end)


function ac.player.__index:send_msg(msg,time)
    if self:is_self() then
        mt:send_msg(msg)
    end
end

